<?php

function runSQL($rsql) {
include('connection.php');
  
    
    $result = mysql_query($rsql) or die ($rsql);
    return $result;
    mysql_close($con);
}
function get_airline_name($str)
{
    $query = mysql_query("SELECT airline_name from airline_new WHERE airline_code='$str' limit 0,1");
    $row=mysql_fetch_array($query);
    if($row[0]!==""){return "&raquo; ".$row[0]."<br>";};
    
}

function get_option($str)
{
    switch($str)
    {
        case "G":
        return "&raquo; Multiple airport<br>";
        break;
        case "H":
        return "&raquo; Direct Access<br>";
        break;
        case "I":
        return "&raquo; Direct flights<br>";
        break;
        case "K":
        return "&raquo; Non Stop Flights<br>";
        break;
        case "L":
        return "&raquo; Single connections orbetter<br>";
        break;
        case "N":
        return "&raquo; SecuRate only<br>";
        break;
        case "O":
        return "&raquo; Flight Service Information<br>";
        break;
        case "G":
        return "&raquo; Multiple airport<br>";
        break;
        case "R":
        return "&raquo; Exclude Double Connections<br>";
        break;
        case "U":
        return "&raquo; Published and Phase I securat<br>";
        break;
        case "V":
        return "&raquo; Phase I SecuRate Only<br>";
        break;
    }
}
function get_status($str)
{
    switch($str)
    {
        case "1":
        return "<span style='color:green;'>Active</span>";
        break;
        case "0":
        return "<span style='color:red;'>Paused</span>";
        break;

    }
}

function countRec($fname,$tname) {
    $sql = "SELECT count($fname) FROM $tname ";
    $result = runSQL($sql);
    while ($row = mysql_fetch_array($result)) {
        return $row[0];
    }    
}
$page = $_POST['page'];
$rp = $_POST['rp'];
$sortname = $_POST['sortname'];
$sortorder = $_POST['sortorder'];

if (!$sortname) $sortname = 'name';
if (!$sortorder) $sortorder = 'desc';

$sort = "ORDER BY $sortname $sortorder";

if (!$page) $page = 1;
if (!$rp) $rp = 10;

$start = (($page-1) * $rp);

$limit = "LIMIT $start, $rp";

$query = $_POST['query'];
$qtype = $_POST['qtype'];

$where = "";
if ($query) $where = " WHERE $qtype LIKE '%$query%' ";

$sql = "SELECT * FROM requests $where $sort $limit";
$result = runSQL($sql);

$total = countRec("idRequests","requests $where");

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
header("Cache-Control: no-cache, must-revalidate" ); 
header("Pragma: no-cache" );
header("Content-type: text/x-json");
$json = "";
$json .= "{\n";
$json .= "page: $page,\n";
$json .= "total: $total,\n";
$json .= "rows: [";
$rc = false;
$i=0;
while ($row = mysql_fetch_array($result)) {
    $inc='';
    $exc='';
    $con_city_cod='';
    $opt=explode(",",$row['options']);
    
     $a=explode(",",$row['inc_arl']);
     if($row['inc_arl']!==""){foreach($a as $b){$inc.=get_airline_name($b);}}
     
     $c=explode(",",$row['exc_arl']);
     
    if($row['exc_arl']!==""){foreach($c as $d){$exc.=get_airline_name($d);}}
      $con_cit=explode(",",$row["con_city_cod"]);
      if($row['con_city_cod']!==""){foreach($con_cit as $d){$con_city_cod.=$d."<br/>";}}
      
    
    $i++;
    if ($rc) $json .= ",";
    $json .= "\n{";
    $json .= "id:'".$i."',";
    $json .= "cell:['".$i."'"; 
    $json .= ",'".get_option($opt[0]).get_option($opt[1]).get_option($opt[2])."'";
    $json .= ",'".addslashes($row['ptc'])."'";
    $json .= ",'".addslashes($inc)."'";
    $json .= ",'".addslashes($exc)."'";
    $json .= ",'".addslashes($con_city_cod)."'";             
    $json .= ",'".addslashes($row['sid'])."'";
    $json .= ",'".addslashes($row['num_alt'])."'";
    $json .= ",'".addslashes(get_status($row['status']))."'";
    $json .= ",'".addslashes("<a href='#' onclick='edit_request(&#39;".$row['idRequests']."&#39;,&#39;".$row['config_id']."&#39;)'><img src='css/table_edit.png' align='absmiddle'  border='0'></a>")."'";
    $json .= ",'".addslashes("<a href='view_req.php?request_id=".$row['idRequests']."&config_id=".$row['config_id']."' target='_blank'><img src='css/page_white_code.png' align='absmiddle'  border='0'></a>")."'";
    $json .= ",'".addslashes("<a href='#' onclick='test_request(&#39;".$row['idRequests']."&#39;,&#39;".$row['config_id']."&#39;)'><img src='css/application_go.png' align='absmiddle'  border='0'></a>")."'";
    $json .= ",'".addslashes("<a href='#' onclick='delete_request(&#39;".$row['idRequests']."&#39;)'><img src='css/table_delete.png' align='absmiddle' border='0'></a>")."']";
    $json .= "}";
    $rc = true; 
           
}
$json .= "]\n";
$json .= "}";
echo $json;
?>